﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using BTGICS.CommonLibrary;
using System.IO;

namespace BTGICS
{
    class Program
    {
        private CompositionContainer _Container; // the composition container for MEF
        #region Public Properties / Members
        [ImportMany]
        public Lazy<IPlugIn, IDictionary<string, object>>[] _modules
        {
            get;
            set;
        } // MEF
        #endregion
        #region Run
        public void Run(string moduleName)
        {
            bool foundit = false;
            bool successfulCompose = Compose();
            if( successfulCompose )
            {
                if( moduleName == "" )
                {
                    DisplayModules();
                }
                else
                {
                    foundit = RunModule( moduleName );
                }
            }
            while( !foundit )
            {
                Console.Write( "module to run >" );
                Console.ForegroundColor = ConsoleColor.Green;
                moduleName = Console.ReadLine();
                Console.ResetColor();
                foundit = RunModule( moduleName );
            }
        }
        #endregion
        #region Display Modules
        private void DisplayModules()
        {
            Console.Clear();
            foreach( var export in _modules )
            {
                IPlugIn module = export.Value as IPlugIn;
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine( "{0}", module.Name );
                Console.ResetColor();
                Console.WriteLine( "\t{0}", module.Description );
            }
        }
        #endregion
        #region Run Module
        private bool RunModule( string moduleName )
        {
            bool foundit = false;
            foreach( var export in _modules )
            {
                IPlugIn module = export.Value as IPlugIn;
                if( module.Name.ToUpper() == moduleName.ToUpper() )
                {
                    foundit = true;
                    module.Run();
                    break;
                }
            }
            return foundit;
        }
        #endregion
        #region Main
        static void Main( string[] args )
        {
            string moduleName = "";
            foreach( string a in args )
            {
                moduleName += a + " ";
            }
            moduleName = moduleName.Trim();
            Program p = new Program();
            p.Run( moduleName );
            Console.WriteLine( "Hit ENTER to quit." );
            Console.ReadLine();
        }
        #endregion
        #region MEF Stuff
        #region GetContainerFromDirectory
        private CompositionContainer GetContainerFromDirectory()
        {
            var catalog = new AggregateCatalog();
            var thisAssembly =
            new AssemblyCatalog(
            System.Reflection.Assembly.GetExecutingAssembly() );

            catalog.Catalogs.Add( thisAssembly );

            catalog.Catalogs.Add(
            new DirectoryCatalog( Directory.GetCurrentDirectory()  ) );

            var container = new CompositionContainer( catalog );
            return container;
        }
        #endregion
        #region Compose
        private bool Compose()
        {
            _Container = GetContainerFromDirectory();

            try
            {
                _Container.ComposeParts( this );
            }
            catch
            ( CompositionException compException )
            {
                Console.WriteLine( compException.ToString() );
                return false;
            }

            return true;
        }
        #endregion
        #endregion
      
    }
}
